package com.superm.controller;

import com.alibaba.fastjson.JSON;
import com.superm.entity.Brand;
import com.superm.entity.RelationShip;
import com.superm.entity.StorgageRack;
import com.superm.service.BrandService;
import com.superm.service.RelationShipService;
import com.superm.service.StorgageRackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 货架控制器
 */
@Controller
@RequestMapping("/storgageRack")
public class StorgageRackController {


    @Autowired
    private StorgageRackService storgageRackService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private RelationShipService relationShipService;
    /**
     * 跳转到货架展示
     * @return
     */
    @RequestMapping("/skipStorgageRack")
    public ModelAndView skipStorgageRack(){
        ModelAndView mv = new ModelAndView("storgageRack/storgageRackList");
        //查询货架信息
        List<StorgageRack> storgageRacksList = storgageRackService.selectStorgageRack(null,null,null);
        mv.addObject("storgageRacksList", JSON.toJSONString(storgageRacksList));
        return mv;
    }

    /**
     * 跳转到添加货架页面
     * @return
     */
    @RequestMapping("/skipStorgageRackAdd")
    public ModelAndView skipStorgageRackAdd(){

        return  new ModelAndView("storgageRack/stogageRackAdd");
    }

    /**
     * 跳转到添加货架页面
     * @return
     */
    @RequestMapping("/skipStorgageRackUpdate")
    public ModelAndView skipStorgageRackUpdate(@RequestParam("storgageRackId") Integer storgageRackId){
        ModelAndView mv = new ModelAndView("storgageRack/stogageRackUpdate");
        StorgageRack storgageRack = storgageRackService.selectIdStogageRack(storgageRackId);
        mv.addObject("storgageRack",storgageRack);
        //查询指定编号的货架信信息
        return  mv;
    }

    /**
     * 返回JSON格式的货架信息
     * @return
     */
    @RequestMapping("/selectJSONIdStorgageRack")
    @ResponseBody
    public Map<String,Object> selectJSONIdStorgageRack(@RequestParam("storgageRackId") Integer storgageRackId){
        Map<String,Object> map = new HashMap<String,Object>();
        StorgageRack storgageRack = storgageRackService.selectIdStogageRack(storgageRackId);
        map.put("data",JSON.toJSONString(storgageRack));
        return map;
    }


    /**
     * 查询货架信息
     * @return
     */
    @RequestMapping("/selectStorgageRack")
    @ResponseBody
    public Map<String,Object> selectStorgageRack(StorgageRack storgageRack, @RequestParam("page")Integer pageNo,@RequestParam("limit")Integer pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        List<StorgageRack> storgageRackList = storgageRackService.selectStorgageRack(storgageRack,pageNo,pageSize);
        //查询条数
        int count = storgageRackService.selectStorgageRackCount(storgageRack);
        map.put("data",storgageRackList);
        map.put("count",count);
        return map;
    }


    /**
     * 异步查询指定货架信息
     * @return
     */
    @RequestMapping("/ajaxSelectStorgageRack")
    @ResponseBody
    public Map<String,Object> ajaxSelectStorgageRack(@RequestParam("storgageRackId") Integer storgageRackId){
        Map<String,Object> map = storgageRackService.ajaxSelectIdStogageRack(storgageRackId);
        return map;
    }

    /**
     * 添加货架信息
     * @return
     */
    @RequestMapping("/addStogageRack")
    @ResponseBody
    public Map<String,Object> addStogageRack(StorgageRack storgageRack){
        Map<String,Object> map = storgageRackService.addStogageRack(storgageRack);
        return map;
    }

    /**
     * 修改货架信息
     * @return
     */
    @RequestMapping("/updateStogageRack")
    @ResponseBody
    public Map<String,Object> updateStogageRack(StorgageRack storgageRack){
        Map<String,Object> map = storgageRackService.updateStogageRack(storgageRack);
        return map;
    }

    /**
     * 删除货架信息
     * @param storgageRackId
     * @return
     */
    @RequestMapping("/deleteStogageRack")
    @ResponseBody
    public Map<String,Object> deleteStogageRack(@RequestParam("storgageRackId")Integer storgageRackId){
        Map<String,Object> map = storgageRackService.deleteStogageRack(storgageRackId);
        return map;
    }


    /**
     * 跳转到显示货架下的商品页面
     * @return
     */
    @RequestMapping("/skipStorgageGoods")
    public ModelAndView skipStorgageGoods(@RequestParam("storgageRack") Integer storgageRack){
        ModelAndView mv = new ModelAndView("storgageRack/storgageRackGoods");
        //查询所有品牌
        List<Brand> listBrand = brandService.selectLikeBrand(null,null,null);
        //查询所有货架
        List<StorgageRack> storgageRacksList = storgageRackService.selectStorgageRack(null,null,null);
        //查询所有一级分类
        List<RelationShip> relationShipList = relationShipService.selectPidMaxRelationShip();
        mv.addObject("storgageRacksList",storgageRacksList);
        mv.addObject("relationShipList",relationShipList);
        mv.addObject("listBrand",listBrand);
        mv.addObject("storgageRack",storgageRack);
        return mv;
    }
}
